package singlelinkedlist;

public class MySingleLinkedList implements IMySingleLinkedList {
    static class ListNode {
        public int val;
        public ListNode next;

        public ListNode(int val) {
            this.val = val;
        }
    }

    //创建头节点head
    public ListNode head;

    @Override
    public void addFirst(int val) {
        //首先要申请一个节点
        ListNode newNode = new ListNode(val);
        newNode.next = head;
        head = newNode;
    }

    @Override
    public void addLast(int val) {
        ListNode newNode = new ListNode(val);
        if (head == null) {
            head = newNode;
            return;
        }
        ListNode current = head;
        while (current.next != null) {
            current = current.next;
        }
        current.next = newNode;
    }

    @Override
    public int size() {
        ListNode current = head;
        int count = 0;
        while (current != null) {
            count++;
            current = current.next;
        }
        return count;
    }

    @Override
    public void addIndex(int pos, int val) {
        //需要判断pos的合法性
        try {
            checkPosLegal(pos);
        } catch (PosNotLegalException e) {
            e.printStackTrace();
        }
        //pos在开头
        if (pos == 0) {
            addFirst(val);
            return;
        }
        //pos在结尾
        if (pos == size()) {
            addLast(val);
            return;
        }
        //pos在中间
        ListNode newNode = new ListNode(val);
        ListNode current = head;
        //找到pos前面一个位置
        while(pos-1 >0){
            current = current.next;
            pos--;
        }
        //进行插入
        newNode.next = current.next;
        current.next = newNode;
    }

    private void checkPosLegal(int pos) throws PosNotLegalException {
        if (pos < 0 || pos > size()) {
            throw new PosNotLegalException("Pos位置不合法...");
        }
    }

    @Override
    public boolean contains(int val) {
        ListNode current = head;
        while(current!=null){
            if(current.val == val){
                return true;
            }
            current = current.next;
        }
        return false;
    }

    @Override
    public void remove(int val) {
        if (!contains(val)){
            System.out.println("你要删除的val不存在！");
            return;
        }
        //在头部
        if(val == head.val){
            head = head.next;
            return;
        }
        //不在头部
        ListNode current = head;
        while(current.next.val != val ){
            current = current.next;
        }
        current.next = current.next.next;
    }

    @Override
    public void removeAllKey(int val) {
        if(head == null){
            return;
        }
        ListNode prev = head;
        ListNode cur = head.next;
        while(cur!=null){
            if(cur.val == val){
                prev.next = cur.next;
            }else{
                prev = prev.next;
            }
            cur =cur.next;
        }
        //处理头节点
        if(head.val == val){
            head = head.next;
        }
    }

    @Override
    public void display() {
        ListNode current = head;
        while (current != null) {
            System.out.print(current.val);
            if (current.next != null) {
                System.out.print("->");
            }
            current = current.next;
        }
        System.out.println();
    }

    @Override
    public void clear() {
        ListNode cur = head;
        while(cur!=null){
            ListNode del = cur;
            cur = cur.next;
            del.next = null;
        }
    }
}
